<script>
export default {
  name: "myinfo",
  data() {
    return {
      form: {
        id: '',
        nickname: '',
        avatar_url: '',
      },
      action: this.$baseUrl + '/api/upload/file',
      fileList: []
    }
  },
  onShow() {
    this.$api.memberDetail(uni.getStorageSync('memberId')).then(res => {
      this.form.id = res.id
      this.form.nickname = res.nickname
      if (res.avatar_url) {
        this.form.avatar_url = res.avatar_url
        this.fileList = [{
          url: this.$baseUrl + res.avatar_url
        }]
      }
    })
  },
  methods: {
    successFile(e) {
      console.log(e)
      this.form.avatar_url = e.data.url
    },
    removeFile() {
      this.form.avatar_url = ''
    },
    submit() {
      this.$api.memberUpdate({
        ...this.form,
      }).then(() => {
        uni.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>

<template>
<div class="myinfo">
  <div class="form-box">
    <u-form :model="form" ref="uForm">
      <u-form-item label="姓名"><u-input v-model="form.nickname" /></u-form-item>
      <u-form-item label="头像">
        <u-upload :action="action" max-count="1" :file-list="fileList" @on-success="successFile" @on-remove="removeFile"></u-upload>
      </u-form-item>
    </u-form>
  </div>
  <u-button type="primary" style="margin-top: 20rpx" @click="submit" >保存</u-button>
</div>
</template>

<style lang="scss">
.myinfo {
  height: 100%;
  background-color: #F5F7FA;
  padding: 20rpx;
  .form-box {
    border-radius: 10px;
    background-color: #FFFFFF;
    padding: 20rpx;
  }
}
</style>